//
//  MGZImgsView.m
//  Demo
//
//  Created by lx on 2019/3/13.
//  Copyright © 2019 mgz. All rights reserved.
//

#import "MGZImgsView.h"

@implementation MGZImgsView

- (instancetype)init{
    if (self = [super init]) {
        [self _initMGZImgsView];
    }
    return self;
}


- (instancetype)initWithCoder:(NSCoder *)aDecoder{
    if (self = [super initWithCoder:aDecoder]) {
        [self _initMGZImgsView];
    }
    return self;
}


- (void)_initMGZImgsView{
    self.imgsEnabled = NO;
    self.backgroundColor = [UIColor whiteColor];
    
}

- (void)setImgsViewLayoutType:(MGZImgsViewLayoutType)imgsViewLayoutType{
    _imgsViewLayoutType = imgsViewLayoutType;
}

- (void)setImgsEnabled:(BOOL)imgsEnabled{
    _imgsEnabled = imgsEnabled;
}

- (void)setImgsArr:(NSMutableArray *)imgsArr{
    _imgsArr = imgsArr;
    
    for (UIImageView *img in self.subviews) {
        [img removeFromSuperview];
    }
    for (int i = 0; i < imgsArr.count; i++) {
        UIImageView *img = [[UIImageView alloc]init];
        [self addSubview:img];
        img.layer.cornerRadius = 4;
        img.userInteractionEnabled = YES;
        img.layer.masksToBounds = YES;
        img.tag = 2000 + i;
        img.backgroundColor = [UIColor redColor];
        UITapGestureRecognizer *tap = [[UITapGestureRecognizer alloc]initWithTarget:self action:@selector(imgViewAction:)];
        [img addGestureRecognizer:tap];
    }
    
    switch (self.imgsViewLayoutType) {
        case MGZImgsViewLayoutTypeNone:
        {
            [self frameLayoutOfNone:imgsArr];
        }
            break;
        case MGZImgsViewLayoutTypeNine:
        {
            [self frameLayoutOfNine:imgsArr];
        }
            break;
            
        default:
        {
            [self frameLayoutOfNone:imgsArr];
        }
            break;
    }
    
}


- (void)frameLayoutOfNone:(NSMutableArray *)imgsArr{
    
    CGFloat itemWith = imgsArr.count == 0 ? 0 :([UIScreen mainScreen].bounds.size.width - 32 - 8) / 3.0f;
    
    [self.subviews mas_distributeSudokuViewsWithFixedItemWidth:itemWith fixedItemHeight:itemWith fixedLineSpacing:4 fixedInteritemSpacing:4 warpCount:5 topSpacing:0 bottomSpacing:0 leadSpacing:16 tailSpacing:16];
    
}

- (void)frameLayoutOfNine:(NSMutableArray *)imgsArr{
    if (imgsArr.count == 0) {
        [self.subviews mas_distributeSudokuViewsWithFixedItemWidth:0 fixedItemHeight:0 fixedLineSpacing:4 fixedInteritemSpacing:4 warpCount:1 topSpacing:0 bottomSpacing:0 leadSpacing:16 tailSpacing:16];
        
    }else if (imgsArr.count == 1) {
        CGFloat itemWidth = 180;
        CGFloat itemHeight = 180;
        CGFloat right = [UIScreen mainScreen].bounds.size.width - itemWidth - 16;
        [self.subviews mas_distributeSudokuViewsWithFixedItemWidth:itemWidth fixedItemHeight:itemHeight fixedLineSpacing:4 fixedInteritemSpacing:4 warpCount:1 topSpacing:0 bottomSpacing:0 leadSpacing:16 tailSpacing:right];
        
        
    }else if (imgsArr.count == 2 || imgsArr.count == 4){
        CGFloat itemWith = ([UIScreen mainScreen].bounds.size.width - 32 - 8) / 3.0f;
        
        CGFloat right = ([UIScreen mainScreen].bounds.size.width - itemWith * 2 - 16 - 4);
        
        [self.subviews mas_distributeSudokuViewsWithFixedItemWidth:itemWith fixedItemHeight:itemWith fixedLineSpacing:4 fixedInteritemSpacing:4 warpCount:2 topSpacing:0 bottomSpacing:0 leadSpacing:16 tailSpacing:right];
    }else{
        CGFloat itemWith = ([UIScreen mainScreen].bounds.size.width - 32 - 8) / 3.0f;
        
        [self.subviews mas_distributeSudokuViewsWithFixedItemWidth:itemWith fixedItemHeight:itemWith fixedLineSpacing:4 fixedInteritemSpacing:4 warpCount:5 topSpacing:0 bottomSpacing:0 leadSpacing:16 tailSpacing:16];
    }
}




- (void)imgViewAction:(UIGestureRecognizer *)tap{
    if (!self.imgsEnabled) {
        return;
    }
    if (self.imgClick) {
        UIImageView *imgView = (UIImageView *)tap.view;
        self.imgClick(imgView.tag - 2000);
    }
    
}

@end
